﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
using System;
namespace WPFExcelReport
{
    public partial class VZUTEI
    {
        public VICC_ABAX VABA
        {
            get
            {
                if (!ENVMAIN.ContainsKey("VABA"))
                    ENVMAIN["VABA"] = new VICC_ABAX();
                return (VICC_ABAX)ENVMAIN["VABA"];
            }
        }
        public VABPOS ABP
        {
            get
            {
                if (!ENVMAIN.ContainsKey("ABP"))
                    ENVMAIN["ABP"] = new VABPOS();
                return (VABPOS)ENVMAIN["ABP"];
            }
        }
        public VABKOPF ABK
        {
            get
            {
                if (!ENVMAIN.ContainsKey("ABK"))
                    ENVMAIN["ABK"] = new VABKOPF();
                return (VABKOPF)ENVMAIN["ABK"];
            }
        }
        public VGPBST GPB
        {
            get
            {
                if (!ENVMAIN.ContainsKey("GPB"))
                    ENVMAIN["GPB"] = new VGPBST();
                return (VGPBST)ENVMAIN["GPB"];
            }
        }
        public VGABEW GBW
        {
            get
            {
                if (!ENVMAIN.ContainsKey("GBW"))
                    ENVMAIN["GBW"] = new VGABEW();
                return (VGABEW)ENVMAIN["GBW"];
            }
        }
        public VGAAUF GD
        {
            get
            {
                if (!ENVMAIN.ContainsKey("GD"))
                    ENVMAIN["GD"] = new VGAAUF();
                return (VGAAUF)ENVMAIN["GD"];
            }
        }
      
        
        public readonly static VZUTEI Instance = new VZUTEI();
        public void NEWZUTEI(Dictionary<string, object> pm)
        {
            dwData = TBMain.NewRow();
            SetUpNewRow(ref dwData);
            string order = pm["ORDERNUM"].ToString();
            string art = pm["CART"].ToString();
            dwData["ZUAUFKRS"] = order.Substring(0, 2);
            dwData["ZUAUFLFD"] = order.Substring(2, 6);
            dwData["ZUAUFPOS"] = order.Substring(8, 3);
            dwData["ZUPOS"] = order.Substring(11, 2);
            dwData["ZUSA"] = order.Substring(13, 3);
            dwData["ZUTEILNR"] = "0000";
            dwData["ZUART"] = art.Substring(0, 20);
            dwData["ZUVAR"] = art.Substring(20, 2);
            dwData["ZUFARBE"] = art.Substring(22, 10);

            dwData["ZUFLAG"] = pm["LPLBKZ"].ToString(); ;
            dwData["ZUPARTIE"] = pm["GPBPARTIE"].ToString(); ;
            dwData["ZUPARTIEKZ"] = pm["GPBPARTIEKZ"].ToString();
            dwData["ZULO"] = pm["LO"].ToString();
            dwData["ZULB"] = pm["LB"].ToString();
            dwData["ZULBEHI"] = pm["GLLBEHI"].ToString();
            dwData["ZUSPPART"] = pm["GPSPPART"].ToString();
            dwData["ZUEINTKG"] = pm["ZUEINTKG"].ToString();
            SResult = QZU11(dwData);
            if(SResult.Tables[0].Rows.Count==0)
            {
                Insert(dwData); 
            }
            else
            {
                UZU03(dwData);
            } 
        }
        public void NEWVKZUTEI(Dictionary<string, object> pm)
        {
            dwData = TBMain.NewRow();
            SetUpNewRow(ref dwData);
            string order = pm["ORDERNUM"].ToString();
            string art = pm["CART"].ToString();
            dwData["ZUAUFKRS"] = order.Substring(0, 2);
            dwData["ZUAUFLFD"] = order.Substring(2, 6);
            dwData["ZUAUFPOS"] = order.Substring(8, 3);
            dwData["ZUPOS"] = order.Substring(11, 2);
            dwData["ZUSA"] = order.Substring(13, 3);
            dwData["ZUTEILNR"] = "0000";
            dwData["ZUART"] = art.Substring(0, 20);
            dwData["ZUVAR"] = art.Substring(20, 3);
            dwData["ZUFARBE"] = art.Substring(23, 10);

            dwData["ZUFLAG"] = "S";
            dwData["ZUPARTIE"] = pm["GPBPARTIE"].ToString(); ;
            dwData["ZUPARTIEKZ"] = "0";
            dwData["ZULO"] = pm["LO"].ToString();
            dwData["ZULB"] = pm["LB"].ToString();
            dwData["ZULBEHI"] = pm["GLLBEHI"].ToString();
            dwData["ZUSPPART"] = pm["GPSPPART"].ToString();
            dwData["ZUEINTKG"] = pm["ZUEINTKG"].ToString();
            dwData["ZUEINTKA"] = "1";
            InsertVKZU(dwData);
        }
        public DataSet LOADZU(Dictionary<string, object> pm)
        {
            dwData = TBMain.NewRow();
            dwData["ZUMANDANT"] = app.Mandant;
            dwData["ZUAUFKRS"] = pm["KRS"];
            dwData["ZUAUFLFD"] = pm["LFD"];
            dwData["ZUAUFPOS"] = pm["POS"];
            SResult = QueryZUTEI(dwData, 1);
            MergeData(SResult);
            return SResult;
        }
        public void Insert(DataRow _dwZU)
        {
            _dwZU["ZUNEUBEN"] = app.User;
            _dwZU["ZUNEUDAT"] = date;
            InsertZUTEI(_dwZU, app.company);
            GBW.NWBBEW(_dwZU, "61", this.RUNID);
            TBMain.Rows.InsertAt(_dwZU, 0);
        }
        public void InsertVKZU(DataRow _dwZU)
        {
            _dwZU["ZUNEUBEN"] = app.User;
            _dwZU["ZUNEUDAT"] = date;
            InsertZUTEI(_dwZU, app.company);
            GBW.NWBBEWVK(_dwZU, "61", this.RUNID);
            TBMain.Rows.InsertAt(_dwZU, 0);
        }
        public void RVZUTEI(Dictionary<string, object> pm)
        {
            dwData = TBMain.NewRow();
            dwData["ZUMANDANT"] = app.Mandant;
            dwData["ZUAUFKRS"] = pm["KRS"];
            dwData["ZUAUFLFD"] = pm["LFD"];
            dwData["ZUAUFPOS"] = pm["POS"];

            DataSet SZUTEI = QueryZUTEI(dwData, 2);
            foreach (DataRow dwZU in SZUTEI.Tables[0].Rows)
            {
                VZUTEI.Instance.DeleteZUTEI(dwZU, 1);
                GBW.NWBBEW(dwZU, "62", this.RUNID);
                DataSet SResult = GPB.QGPB02(dwZU);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    GPB.UGB03(row);
                }
            }
            dwData = GD.TBMain.NewRow();
            dwData["GAMANDANT"] = app.Mandant;
            dwData["GAAUFKRS"] = pm["KRS"];
            dwData["GAAUFLFD"] = pm["LFD"];
            dwData["GAAUFPOS"] = pm["POS"];
            dwData["GASA"] = "713"; 
            GD.UpdateGAAUF(dwData, 25);
            dwData = ABP.TBMain.NewRow();
            dwData["ABPMANDANT"] = app.Mandant;
            dwData["ABPAUFKRS"] = pm["KRS"];
            dwData["ABPAUFLFD"] = pm["LFD"];
            dwData["ABPAUFPOS"] = pm["POS"];
            ABP.UpdateABPOS(dwData, 6);
        }

      

        int ABAID = 1;
        /// <summary>
        /// 1.自动生产生产单所需要的必要数据
        /// 2.利用ICC_ABAX表明生产单之间的计划关系
        /// 3.产生原料需求单
        /// 4.应当根据留位的原料的变量产生
        /// </summary>
        /// <param name="dwInput"></param>
        /// <param name="src"></param>
        /// <returns></returns>
        public DataSet PrePlan(DataSet db, DataRow dwVKP, bool IsRepair)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwABAN;
            string KRS, LFD, POS;
            DataTable tbMU = db.Tables["ICC_ABAX"];      
            string filter = "";
            DataRow[] rows;
            KRS = dwVKP["ABPAUFKRS"].ToString();
            LFD = dwVKP["ABPAUFLFD"].ToString();
            POS = dwVKP["ABPAUFPOS"].ToString();
            decimal SOQTY = Convert.ToDecimal(dwVKP["ABPSOLLMENGE5"].ToString());
            decimal PDQTY = Convert.ToDecimal(dwVKP["ABPSOLLMENGE7"].ToString());
            decimal STKLG = Convert.ToDecimal(dwVKP["ABPSTKLG"].ToString());
            filter = "GAAUFKRS='" + KRS + "' AND GAAUFLFD='" + LFD + "' AND GAAUFPOS='" + POS + "' AND GASA='713'";
            string ABKKDAUFTNR = "";
            foreach (DataRow row in db.Tables["ABKOPF"].Rows)
            {
                ABKKDAUFTNR = row["ABKKDAUFTNR"].ToString();
            }
            rows = db.Tables["GAAUF"].Select(filter);
            filter = "TRIM(GABEM)='*HX*'";
            bool InRepaire = false;
            DataRow[] RHX = db.Tables["GAAUF"].Select(filter);
            if (RHX.Length > 0)
            {
                InRepaire = true;
            }
            foreach (DataRow dwAUF in rows)
            {
                IsRepair = false;
                if (dwAUF["GABEM"].ToString().Trim() == "*HX*")
                    IsRepair = true;
                if (InRepaire)
                {
                    if (!IsRepair) continue;
                }
                dwABAN = tbMU.Rows.Add();
                dwABAN["ABAID"] = ABAID++;
                dwABAN["ABAMANDANT"] = dwAUF["GAMANDANT"];
                dwABAN["ABAAUFTYP"] = "N";
                dwABAN["ABAVKAUFTYP"] = "V";
                if (IsRepair)
                {
                    dwABAN["ABAAUFKRS"] = KRS;
                    dwABAN["ABAAUFLFD"] = LFD;
                }
                else
                {
                    dwABAN["ABAAUFKRS"] = "FP";
                }
                dwABAN["ABAVKAUFKRS"] = KRS;
                dwABAN["ABAVKAUFLFD"] = LFD;
                dwABAN["ABAVKAUFPOS"] = POS;
                dwABAN["EXART"] = ABKKDAUFTNR;
                dwABAN["ABAVKUPOS"] = dwAUF["GAPOS"].ToString();
                if (dwABAN["ABAVKUPOS"].ToString().Trim() == "00")
                {
                    dwABAN["ABAVKUPOS"] = "01";
                }
                dwABAN["ABAAUFMSL"] = dwAUF["GAAUFMSL"].ToString().ToUpper(); //GAAUFMSL
                dwABAN["ABAPACKTYP"] = "BZL ";
                dwABAN["ABAHUELSE"] = " ";
                decimal GAKG1 = Convert.ToDecimal(dwAUF["GAKG1"].ToString());
                decimal GAKG2 = Convert.ToDecimal(dwAUF["GAKG2"].ToString());
                decimal GAKG3 = Convert.ToDecimal(dwAUF["GAKG3"].ToString());
                decimal GAKG5 = Convert.ToDecimal(dwAUF["GAKG5"].ToString());
                decimal GASP1 = Convert.ToDecimal(dwAUF["GASP1"].ToString());
                decimal KG1 = System.Math.Round(GAKG1, 2);
                decimal KG2 = System.Math.Round(GAKG3 + GAKG5, 2);
                decimal SP1 = Convert.ToDecimal(dwAUF["GASP1"].ToString()) / 1000 / 100;
                decimal RATE = SP1 * Math.Round(SOQTY * STKLG / 10000000 / PDQTY, 3);
                decimal RQTY = System.Math.Round(RATE * PDQTY, 0);
                if (GAKG2 > 0)
                {
                    if (!IsRepair)
                    {
                        throw new Exception("纱线:" + dwAUF["GAART"].ToString().Trim() + "-" + dwAUF["GAVAR"].ToString().Trim() + "-" + dwAUF["GAFARBE"].ToString().Trim() + "-" + dwAUF["GAAUFMSL"].ToString() + ",已经分盘，禁止二次进行");
                    }
                }
                if (KG1 != KG2)
                {
                    if (!IsRepair)
                    {
                        throw new Exception("纱线:" + dwAUF["GAART"].ToString().Trim() + "-" + dwAUF["GAVAR"].ToString().Trim() + "-" + dwAUF["GAFARBE"].ToString().Trim() + "-" + dwAUF["GAAUFMSL"].ToString() + ",留位数量" + KG2 / 100 + "不等于需求数量" + KG1 / 100);
                    }
                }
                if ((RQTY - KG1) > 1 || (RQTY - KG1) < -1)
                {
                    if (!IsRepair)
                    {
                        throw new Exception("纱线:" + dwAUF["GAART"].ToString().Trim() + "-" + dwAUF["GAVAR"].ToString().Trim() + "-" + dwAUF["GAFARBE"].ToString().Trim() + "-" + dwAUF["GAAUFMSL"].ToString() + ",按投料比计算需求" + RQTY / 100 + "KG，与需求量" + KG1 / 100 + "不同，请重新计算留位");
                    }
                }
                dwABAN["ABARESMG"] = (GAKG3 + GAKG5) * 10;
                if (IsRepair)
                {
                    dwABAN["ABARESMG"] = (GAKG1) * 10;
                }
                dwABAN["ABAQTYRECEIVED"] = (GAKG5) * 10;
                dwABAN["ABASANZSTK"] = 1;
                dwABAN["ABASSTKGEW"] = dwABAN["ABARESMG"];
                dwABAN["ABAVKART"] = dwABAN["ABARESMG"];
                dwABAN["ABAVKFARBE"] = "100000 ";
                dwABAN["ABACOLORREF"] = Helpers.Helper.Left(dwAUF["GABEM"].ToString().PadRight(10), 10);
                dwABAN["ABAWAKZ"] = "G";
                dwABAN["ABAART"] = dwAUF["GAART"];
                dwABAN["ABAVAR"] = dwAUF["GAVAR"];
                dwABAN["ABAFARBE"] = dwAUF["GAFARBE"];
                dwABAN["ABAVKSTARTD"] = dwAUF["GADATBIS"];
                dwABAN["ABALO"] = dwAUF["GALO"];
                dwABAN["ABALB"] = dwAUF["GALB"];
                dwABAN["ABALP"] = dwAUF["GALP"];
                dwABAN["ABAUSER"] = app.User;
                dwABAN["ABADATE"] = date;
                if (IsRepair)
                {
                    break;//每次仅处理一个回修任务，处理完毕之后，将回修标志删除
                }
            }
            filter = "ABAAUFTYP='N' AND ABAWAKZ='G'";
            rows = tbMU.Copy().Select(filter);
            if (rows.Length > 0)
            {
                foreach (DataRow dwABA in rows)
                {
                    SResult = ABP.PlanFPAB50(db, dwABA, ref ABAID);
                    SReturn.Merge(SResult);
                }
            }
            return SReturn;
        }
    }
}
